import { Injectable, EventEmitter } from "@angular/core";
import { Observable, Observer } from 'rxjs/Rx';

@Injectable()
export class Scope {
  observer: Observer<any>;
  observable: Observable<any> = Observable.create((observer: Observer<any>) => {

    this.observer = observer;

  }).share();



  // constructor() {
  //   this.observable = Observable.create((observer:Observer<any>) => {
  //     this.observer = observer;
  //   }).share();
  // }

  emit(name, data) {

    let json = {
      name: name,
      data: data
    };

    if(this.observer){
      this.observer.next(json);
    }

  }

  on(eventName, callback) {
    return this.observable.filter((event) => {
      return event.name === eventName;
    }).subscribe(callback);
  }


}
